import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/models/response/content_response.dart';
import 'package:sainbainu/common/routes/routes.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/widget.dart';
import 'package:nested_scroll_view_plus/nested_scroll_view_plus.dart';

import 'index.dart';

class RecommendGroupPage extends StatelessWidget {
  const RecommendGroupPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RecommendGroupController>(
      init: RecommendGroupController(),
      builder: (_) {
        return Scaffold(
          body: EasyRefresh.builder(
            controller: _.state.refreshController,
            onRefresh: _.onRefresh,
            onLoad: _.onRichBottom,
            childBuilder: (context, physics) {
              return _buildBody(physics);
            },
          ),
        );
      },
    );
  }

  /// 滚动容器
  Widget _buildBody(ScrollPhysics physics) {
    return NestedScrollViewPlus(
      overscrollBehavior: OverscrollBehavior.outer,
      physics: physics,
      headerSliverBuilder: (context, innerScrolled) {
        return [
          SliverToBoxAdapter(child: _buildGroupList()),
        ];
      },
      body: _buildContentList(physics),
    );
  }

  /// 小组
  Widget _buildGroupList() {
    return GetBuilder<RecommendGroupController>(
      id: 'groupList',
      builder: (_) {
        if (_.state.groupListResponse.rows.isEmpty) {
          return const SizedBox.shrink();
        }
        return SizedBox(
          height: 115,
          child: ListView.separated(
            scrollDirection: Axis.horizontal,
            physics: const BouncingScrollPhysics(),
            itemCount: _.state.groupListResponse.rows.length +
                (_.state.showGroupMore ? 1 : 0),
            padding: const EdgeInsets.all(15),
            separatorBuilder: (context, index) {
              return const SizedBox(width: 10);
            },
            itemBuilder: (context, index) {
              if (_.state.showGroupMore &&
                  index == _.state.groupListResponse.rows.length) {
                return _buildMoreCard();
              }
              var item = _.state.groupListResponse.rows[index];
              return GroupCard(
                item: item,
                onTap: () => _.toGroupDetailPage(
                  item.groupId!,
                  item.uniqueTag,
                  item.cover,
                ),
              );
            },
          ),
        );
      },
    );
  }

  /// 更多
  Widget _buildMoreCard() {
    return GestureDetector(
      onTap: () => Get.toNamed(AppRoutes.groupList),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 8),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5),
        ),
        alignment: Alignment.center,
        child: RotatedBox(
          quarterTurns: 45,
          child: Text(
            '更多'.tr,
            style: const TextStyle(fontSize: 12, color: AppColors.active),
          ),
        ),
      ),
    );
  }

  /// 内容
  Widget _buildContentList(ScrollPhysics physics) {
    return GetBuilder<RecommendGroupController>(
      id: 'contentList',
      builder: (_) {
        return ListView.separated(
          physics: physics,
          itemCount: _.state.contentListResponse.rows.length,
          separatorBuilder: (context, index) {
            return const SizedBox(height: 10);
          },
          itemBuilder: (context, index) {
            ContentResponse item = _.state.contentListResponse.rows[index];
            return GestureDetector(
              onTap: () {
                _.toContentDetailPage(item);
              },
              child: Container(
                color: Colors.white,
                padding: const EdgeInsets.only(
                  top: 20,
                  left: 20,
                  right: 20,
                  bottom: 5,
                ),
                child: UserInfoCell(
                  avatarHeroTag: '${item.uniqueTag}_${item.noteId}',
                  avatar: item.avatar,
                  name: item.customerName ?? '',
                  role: item.officialName,
                  showRoleAuth: item.officialFlag == 1,
                  groupName: item.groupName,
                  region: '${item.provinceName}${item.cityName}',
                  dateTime: item.createTime,
                  onUserTap: () {
                    _.toUserDetailPage(item.customerId);
                  },
                  onGroupTap: () {
                    _.toGroupDetailPage(item.groupId!, null, null);
                  },
                  bottomChild: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      if (item.text != null && item.text!.isNotEmpty)
                        Padding(
                          padding: const EdgeInsets.only(top: 6),
                          child: ContentCardText(item.text!),
                        ),
                      ImageVideoGrid(
                        padding: const EdgeInsets.only(top: 12),
                        imageLinkList: item.image ?? [],
                        videoLink: item.videoCover,
                      ),
                      if (item.audio != null && item.audio!.isNotEmpty)
                        Padding(
                          padding: const EdgeInsets.only(top: 12),
                          child: AudioWaveWidget(
                            duration: item.audioDuration ?? 0,
                          ),
                        ),
                      IconNumRow(
                        giveFlag: item.giveFlag ?? 0,
                        giveNum: item.giveNum ?? 0,
                        collectionFlag: item.collectionFlag ?? 0,
                        collectionNum: item.collectionNum ?? 0,
                        visitNum: item.visitNum ?? 0,
                        commentNum: item.commentNum ?? 0,
                        shareNum: item.shareNum ?? 0,
                        onLikeTap: (isGive) =>
                            _.onGiveHandle(isGive, item.noteId),
                        onCollectionTap: (isGive) =>
                            _.onCollectionHandle(isGive, item.noteId),
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }
}
